new Vue({
    el:"#app",
    data: {
        fullscreenLoading:false,
        screenLoading:false,
        channelData:[],
        dialogFormVisible: false,
        dialogDeleteVisible: false,
        queryContent:{
            keyword:''
        },
        id:null,
        uiType:null,
        targetType:null,
        dialogVisible: false,
        spanArr:[],
        pos:null
    },
    mounted:function(){
        var _this=this;
        _this.loadPlaceList();
    },
    methods:{
        trim:function(str){
            if(isNaN(str)){
                return str.replace(/(^\s*)|(\s*$)/g, "");
            }else{
                return str
            }
        },
        loadPlaceList:function () {
            var _this = this;
            _this.screenLoading=true;
            _this.spanArr = [];
            axios.get(request_prefix+"/cash/place/list",{
                params:{
                    keyword:_this.queryContent.keyword
                }
            })
                .then(function (res) {
                     if(res.data.code === 0) {
                         var data = res.data.data;
                         data.sort(function(o1,o2) {
                             var v1 = o1.channelName;
                             var v2 = o2.channelName;
                             if(v1 > v2) {
                                 return 1
                             } else if(v1 < v2) {
                                 return -1;
                             } else {
                                 return 0;
                             }
                         });
                         _this.channelData = data;
                         for (var i = 0; i < data.length; i++) {
                             if (i === 0) {
                                 _this.spanArr.push(1);
                                 _this.pos = 0
                             } else {
                                 // 判断当前元素与上一个元素是否相同
                                 if (data[i].channelName === data[i - 1].channelName) {
                                     _this.spanArr[_this.pos] += 1;
                                     _this.spanArr.push(0);
                                 } else {
                                     _this.spanArr.push(1);
                                     _this.pos = i;
                                 }
                             }
                         }
                     } else {
                         _this.$message.error("未获得正确数据");
                     }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程出错");
                });
            _this.screenLoading=false;
        },
        cellMerge({ row, column, rowIndex, columnIndex }) {
            if (columnIndex === 0 || columnIndex === 1 || columnIndex == 5) {
                const _row = this.spanArr[rowIndex];
                const _col = _row > 0 ? 1 : 0;
                return {
                    rowspan: _row,
                    colspan: _col
                }
            }
        },
        resetQuery:function () {
            var _this = this;
            _this.queryContent.keyword = '';
            _this.loadPlaceList();
        },
        changeStatus:function (id,uiType,targetType) {
            var _this = this;
            _this.id = parseInt(id);
            _this.uiType = parseInt(uiType);
            _this.targetType = parseInt(targetType);
            _this.dialogFormVisible = true;
        },
        confirmChange:function () {
            var _this = this;
            var data = new URLSearchParams();
            data.append("id",_this.id);
            data.append("uiType",_this.targetType);
            axios.post(request_prefix+"/cash/place/changeType",data)
                .then(function (res) {
                    if(res.data.code === 0) {
                        for (var i = 0; i < _this.channelData.length;i++) {
                            if(parseInt(_this.channelData[i].id) === _this.id) {
                                _this.channelData[i].uiType = _this.targetType;
                            }
                        }
                        _this.dialogFormVisible = false;
                    } else {
                        _this.$message.error("更改失败");
                        _this.dialogFormVisible = false;
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                    _this.dialogFormVisible = false;
                })
        },
        toCreatePage:function () {
            window.location.href = request_prefix + "/cash/place/addPage";
        },
        toNewPage:function (id,channelName,channelCode) {
            var channelInfo = {};
            channelInfo.id = id;
            channelInfo.channelName = channelName;
            channelInfo.channelCode = channelCode;
            localStorage.setItem("channelInfo",JSON.stringify(channelInfo));
            window.location.href = request_prefix + "/cash/place/versionPage";
        },
        toEditPage:function (id) {
            localStorage.setItem("channel_id_",id);
            window.location.href = request_prefix+"/cash/place/editPage";
        },
        toDelete:function (id) {
            var _this = this;
            _this.id = id;
            _this.dialogDeleteVisible = true;
        },
        confirmDelete:function () {
            var _this = this;
            var id = _this.id;
            var data = new URLSearchParams();
            data.append("id",parseInt(id));
            axios.post(request_prefix+"/cash/place/delete",data)
                .then(function (res) {
                    if(res.data.code === 0) {
                        for(var i = 0; i < _this.channelData.length;i++) {
                            if(parseInt(_this.channelData[i].id) == parseInt(id)) {
                                _this.channelData.splice(i,1)
                            }
                        }
                        _this.dialogDeleteVisible = false;
                    _this.loadPlaceList();
                    } else {
                        _this.$message.error("删除失败");
                        _this.dialogDeleteVisible = false;
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                    _this.dialogDeleteVisible = false;
                })
        },
        getStatus:function (status) {
            if(status == 1) {
                return "送审UI";
            } else if(status == 0) {
                return " 用户UI";
            } else if (status == 2) {
                return "cpd审核"
            } else {
                return "错误";
            }
        },

    }
});